package com.jk.xz.service.impl;

import com.jk.AddressBean;
import com.jk.CinemaBean;
import com.jk.MovieHallBean;
import com.jk.xz.dao.CinemaDao;
import com.jk.xz.service.CinemaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
@Service
public class CinemaServiceImpl implements CinemaService {
    @Autowired
    private CinemaDao cinemaDao;
    @Autowired
    private HttpSession session;
    /*
    * @Author: xz
    * @Description: 查询影院
    * @Date: 2021/5/19 16:45
    * @Param:[page, rows, cinema]
    * @Return: java.util.HashMap<java.lang.String,java.lang.Object>
    **/
    @Override
    public HashMap<String, Object> queryCinema(Integer page, Integer rows, CinemaBean cinema) {
            CinemaBean user = (CinemaBean) session.getAttribute("user");
            cinema.setName(user.getName());
            Integer count=cinemaDao.getCount(cinema);
            Integer start=(page-1)*rows;
            List<CinemaBean> list=cinemaDao.queryCinema(start,rows,cinema);
            HashMap<String, Object> map=new HashMap<String, Object>();
            map.put("total",count);
            map.put("rows",list);
            return map;

    }
    //根据影院id查询对映的影厅
    @Override
    public List<MovieHallBean> getHall(Integer id) {

        return cinemaDao.getHall(id);
    }
    /*
    * @Author: xz
    * @Description: 获取市
    * @Date: 2021/5/20 11:05
    * @Param:[]
    * @Return: java.util.List<com.jk.AddressBean>
    **/
    @Override
    public List<AddressBean> getCity() {
        return cinemaDao.getCity();
    }
    //获取县
    @Override
    public List<AddressBean> getCounty(Integer countyId) {

        return cinemaDao.getCounty(countyId);
    }
    /*
     * @Author: xz
     * @Description: 新增影院
     * @Date: 2021/5/20 11:39
     * @Param:
     * @Return:
     **/
    @Override
    public void addCinema(CinemaBean cinema) {
        if (cinema.getCinemaId()==null){
            //新增影院表
            cinema.setPwd("123");
            try {
                // 生成一个MD5加密计算摘要
                MessageDigest md = MessageDigest.getInstance("MD5");
                // 计算md5函数
                md.update(cinema.getPwd().getBytes());
                // digest()最后确定返回md5 hash值，返回值为8位字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
                // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
                //一个byte是八位二进制，也就是2位十六进制字符（2的8次方等于16的2次方）
                String s = new BigInteger(1, md.digest()).toString(16);
                cinema.setPwd(s);
            } catch (Exception e) {
                e.printStackTrace();
            }

            cinemaDao.addCinema(cinema);
            String[] split = cinema.getCinemaServe().split(",");
            //新增影院服务中间表
            cinemaDao.addCinemaService(split,cinema.getCinemaId());
        }
        else{
            try {
                // 生成一个MD5加密计算摘要
                MessageDigest md = MessageDigest.getInstance("MD5");
                // 计算md5函数
                md.update(cinema.getPwd().getBytes());
                // digest()最后确定返回md5 hash值，返回值为8位字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
                // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
                //一个byte是八位二进制，也就是2位十六进制字符（2的8次方等于16的2次方）
                String s = new BigInteger(1, md.digest()).toString(16);
                cinema.setPwd(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
            cinemaDao.upCinema(cinema);
        }
    }

    @Override
    public void delAll(String[] ids) {
        cinemaDao.delAll(ids);
    }
    //回显
    @Override
    public CinemaBean byId(Integer id) {
        return cinemaDao.byId(id);
    }
    /*
    * @Author: xz
    * @Description: 查询影厅
    * @Date: 2021/5/20 15:41
    * @Param:[page, rows, hall]
    * @Return: java.util.HashMap<java.lang.String,java.lang.Object>
    **/
    @Override
    public HashMap<String, Object> queryHall(Integer page, Integer rows, MovieHallBean hall, HttpSession session) {
        CinemaBean user = (CinemaBean) session.getAttribute("user");
        //根据userName查询影院
        CinemaBean cinema= cinemaDao.getCinema2(user.getName());
        //获取影院的id
        Integer cinemaId = cinema.getCinemaId();
        //根据影院的id查询
        Integer count=cinemaDao.getHallCount(hall,cinemaId);
        Integer start=(page-1)*rows;
        List<CinemaBean> list=cinemaDao.queryHall(start,rows,hall,cinemaId);
        HashMap<String, Object> map=new HashMap<String, Object>();
        map.put("total",count);
        map.put("rows",list);
        return map;
    }
    /*
     * @Author: xz
     * @Description: 新增+修改影厅
     * @Date: 2021/5/20 16:28
     * @Param:
     * @Return:
     **/
    @Override
    public void addHall(MovieHallBean hall) {
        CinemaBean user = (CinemaBean) session.getAttribute("user");
        hall.setCinemaId(user.getCinemaId());
        if (hall.getHallId()==null){
            //新增影厅
            cinemaDao.addHall(hall);
        }
        else{
            //先删除对应影厅的座位
            cinemaDao.delSeats(hall);
            cinemaDao.upHall(hall);
        }
        //新增、修改影厅对应座位
        for (int i = 0; i < hall.getRow() ; i++) {
            for (int j = 0; j < hall.getColumn() ; j++) {
                cinemaDao.addSeats((i+1),(j+1),hall);
            }
        }
    }
    /*
     * @Author: xz
     * @Description: 查询影院
     * @Date: 2021/5/20 16:51
     * @Param:[]
     * @Return: java.util.List<com.jk.CinemaBean>
     **/
    @Override
    public List<CinemaBean> getCinema() {
       return  cinemaDao.getCinema();
    }
    /*
     * @Author: xz
     * @Description: 回显hall
     * @Date: 2021/5/20 17:53
     * @Param:
     * @Return:
     **/
    @Override
    public MovieHallBean getHallById(Integer hallId) {
        return cinemaDao.getHallById(hallId);
    }
    /*
    * @Author: xz
    * @Description: 删除影厅
    * @Date: 2021/5/20 19:27
    * @Param:[id]
    * @Return: void
    **/
    @Override
    public void delHall(Integer id) {
        MovieHallBean hall = cinemaDao.getHallById(id);
        cinemaDao.delSeats(hall);
        cinemaDao.delHall(id);
    }
}
